package com.bigcat.gulimall.gulimallware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigcat.gulimall.common.utils.PageUtils;
import com.bigcat.gulimall.common.utils.Query;
import com.bigcat.gulimall.gulimallware.dao.WmsWareSkuDao;
import com.bigcat.gulimall.gulimallware.entity.WmsWareSkuEntity;
import com.bigcat.gulimall.gulimallware.service.WmsWareSkuService;
import com.bigcat.gulimall.gulimallware.vo.SkuHasStockVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wmsWareSkuService")
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuDao, WmsWareSkuEntity> implements WmsWareSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WmsWareSkuEntity> page = this.page(
                new Query<WmsWareSkuEntity>().getPage(params),
                new QueryWrapper<WmsWareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuHasStockVo> getHasStock(List<Long> skuIds) {
        SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
        List<SkuHasStockVo> skuHasStockVoList = skuIds.stream().map(skuId -> {
                    //获得库存数
                    Long count = baseMapper.getHasStock(skuId);
                    //如果大于0为true，反之则为false
                    skuHasStockVo.setHasStock(count != null && count > 0);
                    skuHasStockVo.setSkuId(skuId);
                    return skuHasStockVo;
                }
        ).collect(Collectors.toList());
        //如果为空则返回空串
        if (skuHasStockVoList.size() > 0){
            return new ArrayList<>();
        }
        return skuHasStockVoList;
    }

}